<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
   <head>
      <title>Everrest-ExoContainer Integration Example</title>
   </head>
   <body>
      <h1>EverRest-ExoContainer Integration Example</h1>
      <p>This is sample of integration EverRest framework with ExoContainer.</p>
      <p>We will create simple books service. It should be able give access to books by id, get list all available books 
      and add new book in storage. Service supports JSON format for transfer data to/from client.</p>
      <ul>
	      <li>You need add in web.xml file bootstrap of Book application.
	      <pre>
	      &lt;listener&gt;
	         &lt;listener-class&gt;org.everrest.exoplatform.servlet.EverrestExoContextListener$StandaloneContainerStarter&lt;/listener-class&gt;
	      &lt;/listener&gt;
	      </pre>
	      This is extension of org.everrest.exoplatform.servlet.EverrestExoContextListener that implements method
	      <i>getContainer</i>. This method provides instance of ExoContainer for loads components that may be registered
              in EverRest framework.</li>
	      <li>Then you need add EverRestServlet.
	      <pre>
              &lt;servlet&gt;
                 &lt;servlet-name&gt;EverrestServlet&lt;/servlet-name&gt;
                 &lt;servlet-class&gt;org.everrest.core.servlet.EverrestServlet&lt;/servlet-class&gt;
              &lt;/servlet&gt;
              &lt;servlet-mapping&gt;
                 &lt;servlet-name&gt;EverrestServlet&lt;/servlet-name&gt;
                 &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
              &lt;/servlet-mapping&gt;
	      </pre></li>
	      <li>Add components to ExoContainer. Look at the file <i>src/main/webapp/WEB-INF/classes/conf/exo-configuration.xml</i>.
              That file contains configuration of ExoContainer, in this simple example we added only three components in it.
	      <pre>
		&lt;configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd http://www.exoplaform.org/xml/ns/kernel_1_0.xsd"
xmlns="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd"&gt;

		   &lt;component&gt;
		      &lt;type&gt;org.everrest.sample.exoplatform.BookStorage&lt;/type&gt;
		   &lt;/component&gt;
		   &lt;component&gt;
		      &lt;type&gt;org.everrest.sample.exoplatform.BookService&lt;/type&gt;
		   &lt;/component&gt;
		   &lt;component&gt;
		      &lt;type&gt;org.everrest.sample.exoplatform.BookNotFoundExceptionMapper&lt;/type&gt;
		   &lt;/component&gt;
		&lt;/configuration&gt;
	      </pre>
	      </li>
	      <li>EverRest components.
	      <p><i>org.everrest.sample.exoplatform.Book</i> - is simple Java Bean that will be used to transfer data via JSON.</p>
	      <p><i>org.everrest.sample.exoplatform.BookNotFoundException</i> - exception that will be thrown by
	      <i>org.everrest.sample.exoplatform.BookService</i> if client requested book that does not exist in storage.</p>
	      <p><i>org.everrest.sample.exoplatform.BookNotFoundExceptionMapper</i> - JAX-RS component that intercepts
	      <i>org.everrest.sample.exoplatform.BookNotFoundException</i> and send correct response to client.</p>
	      <pre>
         @Provider
         public class BookNotFoundExceptionMapper implements ExceptionMapper&lt;BookNotFoundException&gt;
         {
            public Response toResponse(BookNotFoundException exception)
            {
               return Response.status(404).entity(exception.getMessage()).type("text/plain").build();
            }
         }
	      </pre>
	      <p><i>org.everrest.sample.exoplatform.BookService</i> - JAX-RS service that process client's requests.
	      This is singleton resource and it inject BookStorage instance via constructor.
	      </p>
	      <p><i>org.everrest.sample.exoplatform.BookStorage</i> - storage of Books. This is singleton component of ExoContainer.</p>
	      <li>Request mapping.
	      <table border="1"><tbody>
	      <tr>
	      <th>Relative Path</th>
         <th>HTTP Method</th>
         <th>Description</th>
	      </tr>
	      <tr>
	      <td>exo-book-service/books/{id}</td>
	      <td>GET</td>
	      <td>Get books with specified id. Just after server start only one book in storage and it can be accessed via
	      id <i>101</i></td>
	      </tr>
	      <tr>
	      <td>exo-book-service/books/</td>
	      <td>GET</td>
	      <td>Get all books from storage.</td>
	      </tr>
	      <tr>
	      <td>exo-book-service/books/</td>
	      <td>PUT</td>
	      <td>Add new book in storage. The body of request must contains book's description in JSON format. The
	      <i>Content-type</i> header must be set to <i>application/json</i></td>
	      </tr>
	      </tbody></table>
	      </li>
	      <li>How to try.
	      <p>Build project.</p>
	      <pre>mvn clean install</pre>
	      <p>Run it with Jetty server.</p>
         <pre>mvn jetty:run</pre>
         <p>Point you web browser to <a href="http://localhost:8080/exo-book-service/books/101">http://localhost:8080/exo-book-service/books/101</a></p>
         <p>If you are under linux or other unix like OS the you can use <i>curl</i> utility (often it is already installed).
         Binary build of this utility available for windows also at <a href="http://curl.haxx.se/download.html">http://curl.haxx.se/download.html</a>.
         With <i>curl</i> you able to add new book in storage via command</p>
         <pre>
         curl -X PUT \
         -H "Content-type:application/json" \
         -d '{"author":"My Author","title":"My Title","price":1.00,"pages":100}' \
         http://localhost:8080/exo-book-service/books/ 
         </pre>
	      </li>
      </ul>
   </body>
</html>
